// mock/system/post.ts

import { Request, Response } from 'express';
import Mock from 'mockjs';

// 模拟帖子数据
const posts: any[] = [
  { id: 1, code: 'POST001', title: '帖子标题1', sort: 10, enable: true, remark: '这是帖子1的备注', createAt: '2023-10-01T12:00:00Z', createBy: 1, updateAt: '2023-10-01T12:00:00Z', updateBy: 1 },
  { id: 2, code: 'POST002', title: '帖子标题2', sort: 20, enable: false, remark: '这是帖子2的备注', createAt: '2023-10-02T12:00:00Z', createBy: 2, updateAt: '2023-10-02T12:00:00Z', updateBy: 2 },
  { id: 3, code: 'POST003', title: '帖子标题3', sort: 30, enable: true, remark: '这是帖子3的备注', createAt: '2023-10-03T12:00:00Z', createBy: 3, updateAt: '2023-10-03T12:00:00Z', updateBy: 3 },
];

// 模拟分页数据
const getPageData = (params: any) => {
  const { current = 1, pageSize = 10 } = params;
  const start = (current - 1) * pageSize;
  const end = start + Number(pageSize);
  return {
    content: posts.slice(start, end),
      totalElements: posts.length,
    current: Number(current),
    pageSize: Number(pageSize),
  };
};

export default {
  // 模拟获取帖子列表接口
  'GET /api/system/post': (req: Request, res: Response) => {
    const pageData = getPageData(req.query);
    res.status(200).json({
      code: 200,
      message: '获取帖子列表成功',
      data: pageData,
    });
  },

  // 模拟获取单个帖子接口
  'GET /api/system/post/:id': (req: Request, res: Response) => {
    const id = parseInt(req.params.id, 10);
    const post = posts.find((post) => post.id === id);
    if (post) {
      res.status(200).json({
        code: 200,
        message: '获取帖子详情成功',
        data: post,
      });
    } else {
      res.status(404).json({
        code: 404,
        message: '帖子不存在',
      });
    }
  },

  // 模拟创建帖子接口
  'POST /api/system/post': (req: Request, res: Response) => {
    const newPost = req.body;
    newPost.id = posts.length + 1;
    posts.push(newPost);
    res.status(200).json({
      code: 200,
      message: '创建帖子成功',
      data: newPost,
    });
  },

  // 模拟更新帖子接口
  'PUT /api/system/post': (req: Request, res: Response) => {
    const updatedPost = req.body;
    const index = posts.findIndex((post) => post.id === updatedPost.id);
    if (index !== -1) {
      posts[index] = { ...posts[index], ...updatedPost };
      res.status(200).json({
        code: 200,
        message: '更新帖子成功',
        data: posts[index],
      });
    } else {
      res.status(404).json({
        code: 404,
        message: '帖子不存在',
      });
    }
  },

  // 模拟删除帖子接口
  'DELETE /api/system/post/:id': (req: Request, res: Response) => {
    const id = parseInt(req.params.id, 10);
    const index = posts.findIndex((post) => post.id === id);
    if (index !== -1) {
      posts.splice(index, 1);
      res.status(200).json({
        code: 200,
        message: '删除帖子成功',
      });
    } else {
      res.status(404).json({
        code: 404,
        message: '帖子不存在',
      });
    }
  },

  // 模拟获取帖子树形结构接口
  'GET /api/system/post/tree': (req: Request, res: Response) => {
    // 这里简单返回全部帖子作为树形结构，实际可按需调整
    res.status(200).json({
      code: 200,
      message: '获取帖子树形结构成功',
      data: posts,
    });
  },

  // 模拟获取帖子选项接口
  'GET /api/system/post/options': (req: Request, res: Response) => {
    const options = posts.map((post) => ({ id: post.id, title: post.title }));
    res.status(200).json({
      code: 200,
      message: '获取帖子选项成功',
      data: options,
    });
  },
};
